package com.example.myapp

import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import android.content.Intent
import android.util.Log
import android.os.Process
import android.app.ActivityManager
import android.content.Context

class MainActivity: FlutterActivity() {
    private val CHANNEL = "com.example.myapp/launcher"
    private val TAG = "MainActivity"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        Log.d(TAG, "配置 Flutter 引擎")
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            Log.d(TAG, "收到方法调用: ${call.method}")
            when (call.method) {
                "launchApp" -> {
                    Log.d(TAG, "收到启动应用请求")
                    try {
                        val intent = Intent(this, MainActivity::class.java)
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or 
                                     Intent.FLAG_ACTIVITY_CLEAR_TOP or
                                     Intent.FLAG_ACTIVITY_SINGLE_TOP)
                        startActivity(intent)
                        Log.d(TAG, "应用启动成功")
                        result.success(true)
                    } catch (e: Exception) {
                        Log.e(TAG, "启动应用失败: ${e.message}")
                        result.error("LAUNCH_ERROR", "启动应用失败", e.message)
                    }
                }
                "restartApp" -> {
                    Log.d(TAG, "收到重启应用请求")
                    try {
                        restartApp()
                        result.success(true)
                    } catch (e: Exception) {
                        Log.e(TAG, "重启应用失败: ${e.message}")
                        result.error("RESTART_ERROR", "重启应用失败", e.message)
                    }
                }
                else -> {
                    Log.d(TAG, "未实现的方法: ${call.method}")
                    result.notImplemented()
                }
            }
        }
    }

    private fun restartApp() {
        Log.d(TAG, "开始重启应用")
        try {
            // 获取当前应用的包名
            val packageName = applicationContext.packageName
            
            // 获取 ActivityManager
            val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            
            // 清除所有任务
            activityManager.killBackgroundProcesses(packageName)
            
            // 创建一个新的启动 Intent
            val intent = Intent(this, MainActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or 
                          Intent.FLAG_ACTIVITY_CLEAR_TASK or
                          Intent.FLAG_ACTIVITY_CLEAR_TOP)
            
            // 启动新的 Activity
            startActivity(intent)
            
            // 结束当前 Activity
            finish()
            
            // 使用 System.exit(0) 而不是 killProcess
            System.exit(0)
        } catch (e: Exception) {
            Log.e(TAG, "重启应用时出错: ${e.message}")
            throw e
        }
    }
}
